package com.zerosky.crm.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.logging.log4j.util.Strings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.zerosky.crm.po.Employee;
import com.zerosky.crm.po.Trip;
import com.zerosky.crm.service.TripService;
import com.zerosky.crm.util.BaseAction;
import com.zerosky.crm.util.PageInfo;

/**
 * 行程相关业务的Controller
 * 
 * @author 钟璟,yanglingchao
 *
 */
@Controller
public class TripController extends BaseAction {
	private Logger logger = LoggerFactory.getLogger(TripController.class); // 日志

	@Resource(name = "tripService")
	private TripService tripService;

	/**
	 * 获取行程列表
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/tripList.do")
	@ResponseBody
	public Map<String, Object> tripList(HttpServletRequest request, HttpSession session, HttpServletResponse response,
			Integer limit, Integer page, String tripName) throws Exception {
		Map<String, Object> json = new HashMap<String, Object>(); // 定义返回map集合
		Employee user = (Employee) session.getAttribute("loginUser");// 获取登录用户信息
		String userId = user.getEmployeeId();
		Map<String, Object> param = new HashMap<String, Object>();// 定义条件集合
		param.put("employeeId", userId);
		param.put("method", "todayTrip");
		PageInfo<Trip> pageInfo = new PageInfo<Trip>();
		try {
			pageInfo.setPageNo(page);
			pageInfo.setPageSize(limit);
			if (Strings.isNotEmpty(tripName)) {
				param.put("tripName", tripName);
			}
			json.put("flag", "0");
			PageInfo<Trip> pageRole = tripService.getTripList(param, pageInfo);
			json.put("total", pageRole.getPageCount());
			json.put("data", pageRole.getList());
			json.put("flag", "1");

		} catch (Exception e) {
			json.put("flag", "-1");
			logger.error("获取数据失败，原因：" + e);
		}
		return json;

	}

	/**
	 * 添加行程记录
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/insertTrip.do")
	@ResponseBody
	public Map<String, Object> insertVisit(Trip trip, String date1, String date2, HttpServletRequest request,
			HttpSession session, HttpServletResponse response) throws Exception {
		Map<String, Object> json = new HashMap<String, Object>();
		try {
			Employee user = (Employee) session.getAttribute("loginUser");// 获取登录用户
			trip.setEmployeeId(user.getEmployeeId());// 写入创建用户名称
			trip.setCreateDate(new Date());
			if (date1 != null) {
				trip.setStartDate(stringToDateTime(date1));
			}
			if (date2 != null) {
				trip.setEndDate(stringToDateTime(date2));
			}
			tripService.saveTrip(trip, "新增");
			json.put("code", 1);
		} catch (Exception e) {
			json.put("code", -1);
			logger.error("获取数据失败，原因：" + e);
		}
		return json;
	}

	/**
	 * 修改行程记录
	 * 
	 * @param trip
	 *            对象
	 * @param request
	 * @param session
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/updateTrip.do")
	@ResponseBody
	public Map<String, Object> updateVisit(Trip trip, String date1, String date2, HttpServletRequest request,
			HttpSession session, HttpServletResponse response) throws Exception {
		Map<String, Object> json = new HashMap<String, Object>();
		try {
			// Employee user = (Employee) session.getAttribute("loginUser");//
			// 获取登录用户
			if (date1 != null) {
				trip.setStartDate(stringToDateTime(date1));
			}
			if (date2 != null) {
				trip.setEndDate(stringToDateTime(date2));
			}
			tripService.saveTrip(trip, "修改");
			json.put("code", 1);
		} catch (Exception e) {
			json.put("code", -1);
			logger.error("获取数据失败，原因：" + e);
		}
		return json;
	}

	/**
	 * 跳转行程界面
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/trip.do")
	public ModelAndView addActivity(String id, HttpServletRequest request, HttpSession session,
			HttpServletResponse response) throws Exception {
		if (id == null || id == "") {// 处理非法请求
			return new ModelAndView("/WEB-INF/jsp/error/404");
		}
		ModelAndView m = new ModelAndView("WEB-INF/jsp/user/tripList");
		return m;
	}

	/**
	 * 获取今日行程
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/todayTrip.do")
	@ResponseBody
	public Map<String, Object> toDayTripList(HttpServletRequest request, HttpSession session,
			HttpServletResponse response) throws Exception {
		Map<String, Object> json = new HashMap<String, Object>(); // 定义返回map集合
		Employee user = (Employee) session.getAttribute("loginUser");// 获取登录用户信息
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String employeeId = user.getEmployeeId();
		Map<String, Object> param = new HashMap<String, Object>();// 定义条件集合
		param.put("employeeId", employeeId);
		param.put("startDate", sdf.format(new Date()));
		param.put("method", "todayTrip");
		PageInfo<Trip> pageInfo = new PageInfo<Trip>();
		try {
			PageInfo<Trip> pageRole = tripService.getTodayTripList(param, pageInfo);
			json.put("data", pageRole.getList());
			json.put("flag", "1");

		} catch (Exception e) {
			json.put("flag", "-1");
			logger.error("获取数据失败，原因：" + e);
		}
		return json;

	}

	/**
	 * 请求编辑行程信息
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/editTrip.do")
	@ResponseBody
	public Map<String, Object> editNotice(String id, HttpServletRequest request, HttpSession session,
			HttpServletResponse response) throws Exception {
		Map<String, Object> json = new HashMap<String, Object>(); // 定义返回map集合
		try {
			Trip trip = tripService.getTripById(id);
			json.put("trip", trip);
			json.put("code", 1);
		} catch (Exception e) {
			json.put("code", -1);
			logger.error("获取数据失败，原因：" + e);
		}
		return json;
	}

	/**
	 * 删除记录
	 * 
	 * @param id
	 *            记录编号
	 * @param request
	 * @param session
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/delTrip.do")
	@ResponseBody
	public Map<String, Object> deleteTripById(String id, HttpServletRequest request, HttpSession session,
			HttpServletResponse response) throws Exception {
		Map<String, Object> json = new HashMap<String, Object>(); // 定义返回map集合
		try {
			tripService.deleteTripById(id);
			json.put("code", 1);
		} catch (Exception e) {
			json.put("code", -1);
			logger.error("获取数据失败，原因：" + e);
		}
		return json;
	}
}
